﻿using HPIT.RentHouse.Common;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.Service.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HPIT.RentHouse.Service
{
    public class PermissionsService : IPermissionsService
    {
        public List<PermissionDTO> GetList()
        {
            var db = new RentHouseEntities();
            BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
            var list = bs.GetList(e => true).Select(e => new PermissionDTO
            {
                Description = e.Description,
                Id = e.Id,
                Name = e.Name
            }).ToList();
            return list;
        }

        /// <summary>
        /// 获取分页列表数据
        /// </summary>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="name"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<PermissionDTO> GetPageList(int start, int length, string name, ref int count)
        {
            using (var db = new RentHouseEntities())
            {
                var bs = new BaseService<T_Permissions>(db);
                var query = PredicateExtensions.True<T_Permissions>();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    query = query.And(e => e.Name.Contains(name));
                }
                var list = bs.GetPagedList(start, length, ref count, query, a => a.Id);
                var result = list.Select(a => new PermissionDTO
                {
                    Id = a.Id,
                    Name = a.Name,
                    Description = a.Description
                }).ToList();
                return result;
            }
        }

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public AjaxResult Add(PermissionDTO dto)
        {
            var db = new RentHouseEntities();
            BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
            T_Permissions per = new T_Permissions();
            per.Name = dto.Name;
            per.Description = dto.Description;
            per.CreateDateTime = DateTime.Now;
            long id=bs.Add(per);
            if (id > 0)
            {
                return new AjaxResult(ResultState.Success, "权限添加成功！");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "权限添加失败！");
            }
        }

        /// <summary>
        /// 根据id获取权限
        /// </summary>
        /// <param name="id">权限id</param>
        /// <returns></returns>
        public PermissionDTO Get(long id)
        {
            var db = new RentHouseEntities();
            BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
            T_Permissions model= bs.Get(a => a.Id == id);
            PermissionDTO dto = new PermissionDTO();
            if (model != null)
            {
                dto.Id = model.Id;
                dto.Name = model.Name;
                dto.Description = model.Description;
            }
            return dto;
        }

        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="permissionDTO"></param>
        /// <returns></returns>
        public AjaxResult Update(PermissionDTO permissionDTO)
        {
            var db = new RentHouseEntities();
            BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
            var model=bs.Get(a => a.Id == permissionDTO.Id);
            model.Name = permissionDTO.Name;
            model.Description = permissionDTO.Description;
            bool res = bs.Update(model);
            if (res)
            {
                return new AjaxResult(ResultState.Success, "权限修改成功！");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "权限修改失败！");
            }
        }

        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="id">权限ID</param>
        /// <returns></returns>
        public AjaxResult Delete(long id)
        {
            var db = new RentHouseEntities();
            BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
            var model = bs.Get(e => e.Id == id);
            if (bs.Delete(model))
            {
                return new AjaxResult(ResultState.Success, "删除权限成功");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "删除权限失败");
            }
        }

        /// <summary>
        /// 批量删除权限
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public AjaxResult DeleteBatch(List<long> ids)
        {
            try
            {
                var db = new RentHouseEntities();
                BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
                foreach (var id in ids)
                {
                    var model = bs.Get(e => e.Id == id);
                    bs.Delete(model);
                }
                return new AjaxResult(ResultState.Success, "删除成功");
            }
            catch (Exception)
            {
                return new AjaxResult(ResultState.Error, "删除失败");
            }
        }

        /// <summary>
        /// 获取用户权限
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<PermissionDTO> GetListByUser(long userId)
        {
            var db = new RentHouseEntities();
            var bs = new BaseService<T_AdminUsers>(db);
            //1、根据用户id获取用户信息
            var user = bs.Get(e => e.Id == userId);
            //2、获取用户所有的角色
            var roleList = user.T_Roles.ToList();
            List<PermissionDTO> permissionList = new List<PermissionDTO>();
            foreach (var item in roleList)
            {
                var rolePermissions = item.T_Permissions.Select(e => new PermissionDTO
                {
                    Id = e.Id,
                    Description = e.Description,
                    Name = e.Name
                }).ToList();
                permissionList.AddRange(rolePermissions);
            }
            return permissionList;
        }
    }
}
